package com.audaque.springboot.foshanupload.core.util;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import org.apache.commons.lang3.RegExUtils;
import org.apache.commons.lang3.StringUtils;

/**
 * @author zgb
 * @desc ...
 * @date 2022-06-07 16:09:45
 */
public class StrUtilExt extends StrUtil {


    /**
     * 半角转全角的函数(SBC case)
     * 全角空格为12288,半角空格为32，其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
     */
    public static String toSbc(Object obj) {
        if (obj == null) {
            return null;
        }
        String input = obj.toString();
        if (isBlank(input)) {
            return "";
        }
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 32) {
                c[i] = (char) 12288;
                continue;
            }
            if (c[i] < 127) {
                c[i] = (char) (c[i] + 65248);
            }
        }
        return new String(c);
    }


    /**
     * 全角转半角的函数(DBC case)
     * 全角空格为12288，半角空格为32 其他字符半角(33-126)与全角(65281-65374)的对应关系是：均相差65248
     */
    public static String toDbc(Object obj) {
        if (obj == null) {
            return null;
        }
        String input = obj.toString();
        if (isBlank(input)) {
            return "";
        }
        char[] c = input.toCharArray();
        for (int i = 0; i < c.length; i++) {
            if (c[i] == 12288) {
                c[i] = (char) 32;
                continue;
            }
            if (c[i] > 65280 && c[i] < 65375) {
                c[i] = (char) (c[i] - 65248);
            }
        }
        return new String(c);
    }

    public static String clearAddress(Object obj) {
        if (obj == null) {
            return null;
        }
        String input = obj.toString();
        String s = toSbc(obj);
        //参数为g,代表替换所有匹配到的字符串
        return StringUtils.upperCase(RegExUtils.replaceAll(s, "[[:space:]]|(^[0-9]+$)|(^[a-zA-Z]+$)", ""));
    }


    public static String clearBlankSingleByte(Object obj) {
        if (obj == null) {
            return null;
        }
        String input = obj.toString();
        String s = toSbc(obj);
        //匹配任何空白字符，包括空格、制表符、换页符等等。等价于 [ \f\n\r\t\v]。
        return RegExUtils.replaceAll(s, "\\s", "");
    }


    public static String clearCompanyName(Object obj) {
        if (obj == null) {
            return null;
        }
        String input = obj.toString();
        return StringUtils.trim(input);
    }


    public static String clearLglName(Object obj) {
        if (obj == null) {
            return null;
        }
        String input = obj.toString();
        String s1 = StringUtils.trim(input);
        String s2 = RegExUtils.replaceAll(s1, "\\([^\\(\\)]*\\)$", "");
        String s3 = RegExUtils.replaceAll(s2, "['||CHR(1) || '-' || CHR(39) || ']", "");
        String s4 = RegExUtils.replaceAll(s3, "[[:punct:]]", "");
        return s4;
    }


    public static String clearMultiPhone(Object obj) {
        if (obj == null) {
            return null;
        }
        String input = obj.toString();
        String s = toSbc(obj);
        String s1 = RegExUtils.replaceAll(s, "(^([[:punct:]]|[[:space:]])+)|(\\-)|(([[:punct:]|[[:space:]])+$)", "");
        String s2 = RegExUtils.replaceAll(s1, "([[:alpha:]]|[[:space:]]|[[:punct:]])+", "");
        String s3 = RegExUtils.replaceAll(s2, "(^0755,)|(^755,)|(^0,)|(^0755)|(^755)|(^0)|(^,+)|(,+$)", "");
        String s4 = RegExUtils.replaceAll(s3, ",+", "");
        if (s4.length() <= 7) {
            return null;
        }
        return s4;
    }


    public static String clearPhoneNew(Object obj) {
        if (obj == null) {
            return null;
        }
        String input = obj.toString();
        String s = toSbc(obj);
        String s1 = RegExUtils.replaceAll(s, "[^-0-9]", "");
        String s2 = RegExUtils.replaceAll(s1, "(^-+)|(-+$)", "");
        boolean isMatch = ReUtil.isMatch("^1(?:3\\d|4[4-9]|5[0-35-9]|6[67]|7[013-8]|8\\d|9\\d)\\d{8}$", s2);

        if (!isMatch) {
            return null;
        }
        return s2;
    }


    public static String clearIdentityNoNew(Object obj) {
        if (obj == null) {
            return null;
        }
        String input = obj.toString();

        String s = toSbc(StringUtils.upperCase(input));
        String s1 = replace(s, "<", "(");
        String s2 = replace(s1, ">", ")");
        String s3 = RegExUtils.replaceAll(s2, "([^0-9A-Z\\(\\)])", "");
        String s4 = replace(s3, "()", "");
        String s5 = RegExUtils.replaceAll(s4, "(^(,|\\)|[[:space:]])+)|((,|\\(|[[:space:]])+$)", "");
        return s5;
    }


    public static String clearNameNew(Object obj) {
        if (obj == null) {
            return null;
        }
        String input = obj.toString();

        String s = toSbc(input);
        boolean match = ReUtil.isMatch("^([a-zA-Z0-9]|[[:punct:]]|[[:space:]])+$", s);
        String result = null;
        if (match) {
            String s1 = RegExUtils.replaceAll(s, "[[:punct:]]|[[:space:]]|[[:digit:]]", "");
            String s2 = StringUtils.upperCase(s1);
            result = s2;
        } else {
            String s1 = RegExUtils.replaceAll(s, "[a-zA-Z0-9]|[[:punct:]]|[[:space:]]", "");
            result = s1;
        }
        return result;
    }


    public static String clearEmail(Object obj) {
        if (obj == null) {
            return null;
        }
        String input = obj.toString();

        boolean isMatch = ReUtil.isMatch("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$", input);

        if (!isMatch) {
            return null;
        }
        return input;

    }


    public static String clearPostcode(Object obj) {
        if (obj == null) {
            return null;
        }
        String input = obj.toString();
        String s = toSbc(input);
        String s1 = RegExUtils.replaceAll(s, "[^-0-9]", "");
        boolean isMatch = ReUtil.isMatch("^[1-9]\\d{5}$", s1);

        if (!isMatch) {
            return null;
        }
        return s1;
    }


    public static String clearYear(Object obj) {
        if (obj == null) {
            return null;
        }
        String input = obj.toString();
        String s = toSbc(input);
        String s1 = RegExUtils.replaceAll(s, "[^-0-9年]", "");
        boolean isMatch = ReUtil.isMatch("^[1-2]{1}[0-9]{3}年?$", s1);

        if (!isMatch) {
            return null;
        }
        return s1;

    }


    public static String clearHight(Object obj) {
        if (obj == null) {
            return null;
        }
        String input = obj.toString();
        String s = toSbc(input);
        String s1 = StringUtils.upperCase(s);
        String s2 = RegExUtils.replaceAll(s1, "[^.0-9CM]", "");
        boolean isMatch = ReUtil.isMatch("^(?:[3-9][0-9]{1}(?:\\.\\d{1,2})?|[1-2][0-9]{2}(?:\\.\\d{1,2})?)(CM)?$", s2);

        if (!isMatch) {
            return null;
        }
        return s2;

    }


    public static String clearAge(Object obj) {
        if (obj == null) {
            return null;
        }
        String input = obj.toString();
        String s = toSbc(input);
        String s1 = RegExUtils.replaceAll(s, "[^0-9]", "");
        boolean isMatch = ReUtil.isMatch("^[1-9]$|^[1-9][0-9]$|^1[0-2][0-9]$", s1);

        if (!isMatch) {
            return null;
        }
        return s1;

    }


    public static String clearMdcCode(Object obj) {
        if (obj == null) {
            return null;
        }
        String input = obj.toString();
        String s = toSbc(input);
        String s1 = RegExUtils.replaceAll(s, "[^0-9a-zA-Z]", "");
        boolean isMatch = ReUtil.isMatch("^[0-9a-zA-Z]{9}$", s1);

        if (!isMatch) {
            return null;
        }
        return s1;

    }


    public static String clearScuCode(Object obj) {
        if (obj == null) {
            return null;
        }
        String input = obj.toString();
        String s = toSbc(input);
        String s1 = RegExUtils.replaceAll(s, "[^0-9]", "");
        boolean isMatch = ReUtil.isMatch("^[0-9]{7}$|^[0-9]{9}$", s1);

        if (!isMatch) {
            return null;
        }
        return s1;

    }


    public static String clearHosCode(Object obj) {
        if (obj == null) {
            return null;
        }
        String input = obj.toString();
        String s = toSbc(input);
        String s1 = RegExUtils.replaceAll(s, "[^0-9T]", "");
        boolean isMatch = ReUtil.isMatch("^4403([0-2])([0-9])[0-9]{8}(T|[0-9])[0-9]{10}$", s1);

        if (!isMatch) {
            return null;
        }
        return s1;

    }


    public static String clearBudCode(Object obj) {
        if (obj == null) {
            return null;
        }
        String input = obj.toString();
        String s = toSbc(input);
        String s1 = RegExUtils.replaceAll(s, "[^0-9T]", "");
        boolean isMatch = ReUtil.isMatch("^4403([0-2])([0-9])[0-9]{8}(T|[0-9])[0-9]{4}$", s1);

        if (!isMatch) {
            return null;
        }
        return s1;

    }


    public static String clearPrpCode(Object obj) {
        if (obj == null) {
            return null;
        }
        String input = obj.toString();
        String s = toSbc(input);
        boolean isMatch = ReUtil.isMatch("^深房地字第[0-9]{10}号$|^[0-9]{10}$", s);

        if (!isMatch) {
            return null;
        }
        return s;

    }


    public static String onlyKeepNums(Object obj) {
        if (obj == null) {
            return null;
        }
        String input = obj.toString();
        String s = toSbc(input);
        String s1 = RegExUtils.replaceAll(s, "[^0-9.]", "");
        return s1;

    }


    public static String cutBud19Code(Object obj) {
        if (obj == null) {
            return null;
        }
        String input = obj.toString();
        String s = toSbc(input);
        String s1 = RegExUtils.replaceAll(s, "[^0-9T]", "");
        StringUtils.substring(s1, 0, 19);
        return s1;

    }


    public static String clearCreditCode(Object obj) {
        if (obj == null) {
            return null;
        }
        String input = obj.toString();
        String s = toSbc(input);
        String s1 = StringUtils.upperCase(s);
        boolean isMatch = ReUtil.isMatch("^([0-9A-HJ-NPQRTUWXY]{2}\\d{6}[0-9A-HJ-NPQRTUWXY]{10}|[1-9]\\d{14}|[0-9A-HJ-NPQRTUWXY]{8}-?[0-9A-HJ-NPQRTUWXY])$", s1);

        if (!isMatch) {
            return null;
        }
        return s1;

    }


    public static String clearTime(Object obj) {
        if (obj == null) {
            return null;
        }
        String input = obj.toString();
        String s = clearBlankSingleByte(obj);

        String result = null;
        if (ReUtil.isMatch("^([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])-([1-9]|0[1-9]|1[0-2])月-[0-9]{2}$", s)) {
            DateTime parse = DateUtil.parse(s, "dd-MM月-yy");
            result = DateUtil.format(parse, "yyyy-MM-dd hh24:mi:ss");
        } else if (ReUtil.isMatch("^\\d{4}-([1-9]|0[1-9]|1[0-2])-([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])$", s)) {
            DateTime parse = DateUtil.parse(s, "yyyy-MM-dd");
            result = DateUtil.format(parse, "yyyy-MM-dd hh24:mi:ss");
        } else if (ReUtil.isMatch("^\\d{4}.([1-9]|0[1-9]|1[0-2]).([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])$", s)) {
            DateTime parse = DateUtil.parse(s, "yyyy.MM.dd");
            result = DateUtil.format(parse, "yyyy-MM-dd hh24:mi:ss");
        } else if (ReUtil.isMatch("^\\d{4}-([1-9]|0[1-9]|1[0-2])-([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1]) \\d{2}:\\d{2}:\\d{2}(\\.\\d+)?$", s)) {
            DateTime parse = DateUtil.parse(s, "yyyy-MM-dd hh24:mi:ss.ms");
            result = DateUtil.format(parse, "yyyy-MM-dd hh24:mi:ss");
        } else if (ReUtil.isMatch("^\\d{4}/([1-9]|0[1-9]|1[0-2])/([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])$", s)) {
            DateTime parse = DateUtil.parse(s, "yyyy/MM/dd");
            result = DateUtil.format(parse, "yyyy-MM-dd hh24:mi:ss");
        } else if (ReUtil.isMatch("^([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])-([1-9]|0[1-9]|1[0-2])月-\\d{2}\\d{2}\\.\\d{2}\\.\\d{2}\\.\\d{9}上午$", s)) {
            boolean isMatch = ReUtil.isMatch("([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])-([1-9]|0[1-9]|1[0-2])月-\\d{2}\\d{2}\\.\\d{2}\\.\\d{2}", s);

            if (!isMatch) {
                return null;
            }

            DateTime parse = DateUtil.parse(s, "dd-MM月-yyhh24.mi.ss");
            result = DateUtil.format(parse, "yyyy-MM-dd hh24:mi:ss");
        } else if (ReUtil.isMatch("^([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])-([1-9]|0[1-9]|1[0-2])月-\\d{2}\\d{2}\\.\\d{2}\\.\\d{2}\\.\\d{9}下午$", s)) {
            boolean isMatch = ReUtil.isMatch("([1-9]|0[1-9]|1[0-9]|2[0-9]|3[0-1])-([1-9]|0[1-9]|1[0-2])月-\\d{2}\\d{2}\\.\\d{2}\\.\\d{2}", s);

            if (!isMatch) {
                return null;
            }
            DateTime parse = DateUtil.parse(s, "dd-MM月-yyhh24.mi.ss");
            DateTime dateTime = DateUtil.offsetHour(parse, 12);
            result = DateUtil.format(dateTime, "yyyy-MM-dd hh24:mi:ss");
        } else if (ReUtil.isMatch("^\\d{5}$", s)) {
            int i = Integer.parseInt(s) - 2;
            DateTime parse = DateUtil.parse("1900-01-01", "yyyy-MM-dd");
            DateTime dateTime = DateUtil.offsetDay(parse, i);
            result = DateUtil.format(dateTime, "yyyy-MM-dd hh24:mi:ss");

        } else if (ReUtil.isMatch("^(?:19|20)\\d{2}(0[1-9]|1[0-2])(0[1-9]|1[0-9]|2[0-9]|3[0-1])$", s)) {
            DateTime parse = DateUtil.parse(s, "yyyyMMdd");
            result = DateUtil.format(parse, "yyyy-MM-dd hh24:mi:ss");

        }

        return result;

    }


    public static String clearTimeKeep(Object obj) {
        if (obj == null) {
            return null;
        }
        String input = obj.toString();
        String s = clearBlankSingleByte(obj);

        String result = null;
        String s1 = clearTime(obj);
        if (s1 != null) {
            result = s1;
        } else {
            if (s == null) {
                result = null;
            } else {
                result = s + "_null";
            }
        }

        return result;

    }


    public static String onlyKeepCn(Object obj) {
        if (obj == null) {
            return null;
        }
        String input = obj.toString();
        return RegExUtils.replaceAll(input, "[^\\\\u4e00-\\\\u9fa5]", "");
    }


    public static String onlyKeepEn(Object obj) {
        if (obj == null) {
            return null;
        }
        String input = obj.toString();
        return RegExUtils.replaceAll(input, "[^a-zA-z]", "");
    }


}
